/**   
 * �ļ���UserService.java</br>
 * ������ </br>
 * ������Ա�����Ʋ� </br>
 * ����ʱ�䣺 2016-4-17
 */ 

package com.user.service;

import java.util.ArrayList;
import java.util.List;

import com.base.util.AbstractEntity;
import com.base.util.Page;
import com.post.bean.Post;
import com.user.bean.BBSUser;
import com.user.bean.UserCollect;
import com.user.bean.UserFocus;
import com.user.dao.UserDAO;

/**
 * 类名: UserService</br> 
 * 包名：com.user.service </br> 
 * 描述: </br>
 * 发布版本号：</br>
 * 开发人员： 汪浩博</br>
 * 创建时间： 2016-5-4
 */

public class UserService {
	
	private UserDAO userDAO;
	
	public UserDAO getUserDAO() {
		return userDAO;
	}

	public void setUserDAO(UserDAO userDAO) {
		this.userDAO = userDAO;
	}
	/**
	 * 方法名: findUserById</br>
	 * 详述: 通过id查找用户</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-2</br>
	 * @param uid
	 * @return
	 */
	public BBSUser findUserById(String uid){
		BBSUser user = null;
		String hql = "from BBSUser where id= '"+uid+"'";
		try {
			user = (BBSUser) userDAO.findByHql(hql).get(0);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return user;
	}
	/**
	 * 方法名: Querypage</br>
	 * 详述: 分页查询</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-3</br>
	 * @param user
	 * @param pagenum
	 * @param pagesize
	 * @param date
	 * @param sort
	 * @return
	 */
	public Page Querypage(BBSUser user, int pagenum, int pagesize){
		String queryString = this.Sethql(user);
		Page page = null;
		page = userDAO.getPage(queryString, pagenum, pagesize);
		return page;
	}
	
	/** 
	 * 方法名: Sethql</br>
	 * 详述: 注入hql</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-3</br>
	 * @param user
	 * @return
	 */ 
	public String Sethql(BBSUser user) {
		StringBuilder queryString = new StringBuilder("from BBSUser b where 1 = 1 ");
		if(user != null){
			if(user.getNickName() != null && !"".equals(user.getNickName())){
				queryString.append(" and b.nickName = '").append(user.getNickName()).append("'");
			}
			if(user.getProvinces() != null && !"".equals(user.getProvinces())){
				queryString.append(" and b.provinces = '").append(user.getProvinces()).append("'");
			}
			if(user.getStatus() != null && !"".equals(user.getStatus())){
				if(user.getStatus().equals("-1")){
					//只有禁言的加条件
					queryString.append(" and b.status = '").append(user.getStatus()).append("'");
				}
			}
		}
		return queryString.toString();
	}

	/**
	 * 方法名: checkUser</br>
	 * 详述:检查用户是否存在</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-5</br>
	 * @param user
	 * @return true表示已经有用户
	 */
	public boolean checkUser(BBSUser user){
		boolean flag = false;
		String hql = "from BBSUser where userName = ? ";
		Object[] params = {user.getUserName()};
		ArrayList userList = new ArrayList();
		try {
			userList = (ArrayList) userDAO.findByHql(hql, params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(userList.size() > 0){
			//已经有此用户
			flag = true;
		}
		return flag;
	}
	/**
	 * 方法名: addUser</br>
	 * 详述: 新增用户</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-5</br>
	 * @param user
	 * @return	true表示新增成功
	 */
	public boolean addUser(BBSUser user){
		boolean flag = false;
		try {
			userDAO.insert(user);
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 方法名: Login</br>
	 * 详述: 登录</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-5</br>
	 * @param user
	 * @return 返回的user不为null 表示找到了用户
	 */
	public BBSUser Login(BBSUser user){
		BBSUser loginUser = null;
		String hql = "from BBSUser where userName = ? and password = ?";
		Object[] params = {user.getUserName(),user.getPassword()};
		ArrayList userList = new ArrayList();
		try {
			userList = (ArrayList) userDAO.findByHql(hql, params);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if(userList.size() > 0){
			//找到了用户
			loginUser = (BBSUser) userList.get(0);
		}
		return loginUser;
	}
	
	/**
	 * 方法名: updateUser</br>
	 * 详述: 更新用户</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-6</br>
	 * @param user
	 * @return true表示更新成功
	 */
	public boolean updateUser(BBSUser user){
		boolean flag = false;
		try {
			userDAO.saveOrUpdate(user);
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	/** 
	 * 方法名: addCollect</br>
	 * 详述: 收藏帖子,新增收藏记录</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-21</br>
	 */ 
	public boolean addCollect(UserCollect collect) {
		boolean flag = false;
		try {
			userDAO.saveOrUpdate(collect);
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}

	/** 
	 * 方法名: checkCollectExist</br>
	 * 详述: 检查是否已经收藏</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-21</br>
	 * @param collect
	 * @return true 表示存在
	 */ 
	public boolean checkCollectExist(UserCollect collect) {
		boolean exist = true;
		List list = new ArrayList();
		String hql = "from UserCollect where userid ='"+collect.getUserid()+"' and pid ='"+collect.getPid()+"'";
		try {
			list = userDAO.findByHql(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (list.size() < 1) {
			exist = false;
		}
		return exist;
	}

	/** 
	 * 方法名: cancelCollect</br>
	 * 详述: 取消收藏</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-5-21</br>
	 * @param pid
	 * @return true 表示正常取消收藏
	 */ 
	public boolean cancelCollect(String pid) {
		boolean flag = false;
		String hql = "delete from UserCollect where pid = '"+pid+"'";
		try {
			userDAO.updateHql(hql);
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 方法名: addFocus</br>
	 * 详述: 关注用户，新增关注记录</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-10</br>
	 * @param focus
	 * @return true 表示成功
	 */
	public boolean addFocus(UserFocus focus) {
		boolean flag = false;
		try {
			userDAO.saveOrUpdate(focus);
			flag = true;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return flag;
	}
	/**
	 * 方法名: checkFocusExist</br>
	 * 详述: 检查关注是否存在</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-10</br>
	 * @param focus
	 * @return  true 表示存在
	 */
	public boolean checkFocusExist(UserFocus focus){
		boolean exist = true;
		List list = new ArrayList();
		String hql = "from UserFocus where userid ='"+focus.getUserid()+"' and focusUserid ='"+focus.getFocusUserid()+"'";
		try {
			list = userDAO.findByHql(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
		if (list.size() < 1) {
			exist = false;
		}
		return exist;
	}
	
	/**
	 * 方法名: findFocusUserById</br>
	 * 详述: 通过用户id查询相关关注的用户,分页的</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-10</br>
	 * @param uid
	 * @param pagenum
	 * @param pagesize
	 * @return
	 */
	public Page findFocusUserById(Long uid, Integer pagenum, Integer pagesize){
		Page page = null;
		String queryString = "from BBSUser where id in (select b.focusUserid from UserFocus b where b.userid = '"+uid+"') ";
		page = userDAO.getPage(queryString, pagenum, pagesize);
		return page;
	}
	/**
	 * 方法名: findFansUserById</br>
	 * 详述: 查询某用户的粉丝，分页的</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-11</br>
	 * @param uid
	 * @param pagenum
	 * @param pagesize
	 * @return
	 */
	public Page findFansUserById(Long uid, Integer pagenum, Integer pagesize){
		Page page = null;
		String queryString = "from BBSUser where id in (select b.userid from UserFocus b where b.focusUserid = '"+uid+"') ";
		page = userDAO.getPage(queryString, pagenum, pagesize);
		return page;
	}
	/**
	 * 方法名: cancelFocus</br>
	 * 详述: 取消关注</br>
	 * 开发人员：汪浩博</br>
	 * 创建时间：2016-6-12</br>
	 * @param id
	 * @param bbsUserId
	 */
	public void cancelFocus(Long id, String bbsUserId){
		String hql = "delete from UserFocus where userid = '"+id+"' and focusUserid = '"+bbsUserId+"'";
		try {
			userDAO.updateHql(hql);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
}
